<?php

/**
 * This is the model class for table "{{product_post}}".
 *
 * The followings are the available columns in table '{{product_post}}':
 * @property string $id
 * @property string $channel_id
 * @property string $title
 * @property string $code
 * @property string $summary
 * @property string $content
 * @property string $price
 * @property string $image_link
 * @property string $create_time
 * @property string $update_time
 * @property string $sort
 * @property integer $visible
 * @property string $view_count
 */
class ProductPost extends CActiveRecord
{
	const THUMB_PATH = 'upload/product';
	/**
	 * Returns the static model of the specified AR class.
	 * @return ProductPost the static model class
	 */
	public static function model($className=__CLASS__) {return parent::model($className);}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {return '{{product_post}}';}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// Define only rules for those attributes that will receive user inputs.
		return array(
			array('title, create_time, update_time', 'required'),
			array('visible', 'numerical', 'integerOnly'=>true),
			array('channel_id, create_time, update_time, sort, view_count', 'length', 'max'=>10),
			array('title', 'length', 'max'=>500),
			array('code, image_link', 'length', 'max'=>255),
			array('price', 'length', 'max'=>20),
			array('summary, content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, channel_id, title, code, summary, content, price, image_link, create_time, update_time, sort, visible, view_count', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
			'channel'=>array(self::BELONGS_TO,'ProductChannel','channel_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 	=> Yii::t('productpost', 'ID'),
			'channel_id' 	=> Yii::t('productpost', 'Danh mục sản phẩm'),
			'title' 	=> Yii::t('productpost', 'Tên sản phẩm'),
			'code' 	=> Yii::t('productpost', 'Mã'),
			'summary' 	=> Yii::t('productpost', 'Tóm tắt'),
			'content' 	=> Yii::t('productpost', 'Nội dung'),
			'price' 	=> Yii::t('productpost', 'Giá'),
			'image_link' 	=> Yii::t('productpost', 'Hình ảnh'),
			'create_time' 	=> Yii::t('productpost', 'Ngày tạo'),
			'update_time' 	=> Yii::t('productpost', 'Ngày cập nhật'),
			'sort' 	=> Yii::t('productpost', 'Thứ tự'),
			'visible' 	=> Yii::t('productpost', 'Ẩn/Hiện'),
			'view_count' 	=> Yii::t('productpost', 'Số lượt xem'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search($cri=null)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.channel_id',$this->channel_id,true);
		$criteria->compare('t.title',$this->title,true);
		$criteria->compare('t.code',$this->code,true);
		$criteria->compare('t.summary',$this->summary,true);
		$criteria->compare('t.content',$this->content,true);
		$criteria->compare('t.price',$this->price,true);
		$criteria->compare('t.image_link',$this->image_link,true);
		$criteria->compare('t.create_time',$this->create_time,true);
		$criteria->compare('t.update_time',$this->update_time,true);
		$criteria->compare('t.sort',$this->sort,true);
		$criteria->compare('t.visible',$this->visible);
		$criteria->compare('t.view_count',$this->view_count,true);
		if($cri!==null) $criteria->mergeWith($cri);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'t.sort ASC, t.id DESC'),
			'pagination'=>array('pageSize'=>Yii::app()->session->get('backendPageSize',25)),
		));
	}

	public function getThumbS() 
	{

		$str = str_replace('.','_S.', $this->image_link);
		if(file_exists($this->image_link))
		{
			if(!file_exists($str))
			{
				NImage::resizeAndCropRect($this->image_link, $str,81,81);
			}
		}
		return $str;
	}

	public function getThumbM() 
	{

		$str = str_replace('.','_M.', $this->image_link);
		if(file_exists($this->image_link))
		{
			if(!file_exists($str))
			{
				NImage::resizeAndCropRect($this->image_link, $str,81,81);
			}
		}
		return $str;
	}

	public function getThumbL() 
	{

		$str = str_replace('.','_L.', $this->image_link);
		if(file_exists($this->image_link))
		{
			if(!file_exists($str))
			{
				NImage::resizeAndCropRect($this->image_link, $str,81,81);
			}
		}
		return $str;
	}

	public function afterDelete()
	{
		@unlink($this->image_link);
		@unlink($this->thumbS);
		@unlink($this->thumbM);
		@unlink($this->thumbL);
		
		parent::afterDelete();
	}
}